﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Navigation;
using SL4_MVVM_MEF;

namespace SL4_MVVM_MEF_Navigation
{
    public partial class MEFModuleContainer : Page, IPartImportsSatisfiedNotification
    {
        private Uri _requestedUri;

        public MEFModuleContainer()
        {
            InitializeComponent();

            /* Use PackageCatalog
            var catalog = new PackageCatalog();
            catalog.AddPackage(Package.Current);

            Package.DownloadPackageAsync(new Uri("MEFModule.xap", UriKind.Relative),
            (sender, pkg) =>
            {
                catalog.AddPackage(pkg);
                var container = new CompositionContainer(catalog);
                //container.ComposeExportedValue(catalog);
                //CompositionHost.InitializeContainer(container);
                container.ComposeParts(this);
            });
            */
        }

        [Import]
        public IDeploymentCatalogService CatalogService { get; set; }

        [ImportMany(AllowRecomposition = true)]
        public Lazy<UserControl, IMetadataContent>[] MEFModuleList { get; set; }


        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            _requestedUri = e.Uri;

            CompositionInitializer.SatisfyImports(this);
        }

        #region IPartImportsSatisfiedNotification Members

        /// <summary>
        /// Executed when all imports are satisfied
        /// </summary>
        public void OnImportsSatisfied()
        {
            if (_requestedUri == new Uri("/MEFModule", UriKind.RelativeOrAbsolute))
            {
                CatalogService.AddXap("MEFModule.xap");
                MEFModuleList.ToList()
                    .ForEach(module =>
                    {
                        if ((module.Metadata.ContainerType == this.GetType().ToString() &&
                            (module.Value.GetType().ToString() == "SL4_MVVM_MEF.MEFModule.Views.SampleView")))
                        {
                            content.Items.Add(module.Value);
                        }
                    });
            }

            if (_requestedUri == new Uri("/WCFRIAModule", UriKind.RelativeOrAbsolute))
            {
                CatalogService.AddXap("WcfRIAServicesModule.xap");
                MEFModuleList.ToList()
                    .ForEach(module =>
                    {
                        if ((module.Metadata.ContainerType == this.GetType().ToString()) &&
                            (module.Value.GetType().ToString() == "SL4_MVVM_MEF.WcfRIAServicesModule.Views.SampleWCFRiaView"))
                        {
                            content.Items.Add(module.Value);
                        }
                    });
            }
        }

        #endregion
    }
}